/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.woxthebox.draglistview.sample.adapter;

import com.woxthebox.draglistview.basecomponent.BaseDependentLayout;
import com.woxthebox.draglistview.minterface.MyObserver;
import com.woxthebox.draglistview.sample.ResourceTable;
import com.woxthebox.draglistview.sample.bean.ListBean;
import com.woxthebox.draglistview.utils.FinalStaticBean;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

import java.util.List;

/**
 * 列表类适配器
 *
 * @since 2021-04-19
 */
public class ListAdapter extends BaseItemProvider {
    private List<ListBean> listBeans;
    private AbilitySlice slice;
    private int mLayoutType;
    private ShapeElement vectorElement;
    private MyObserver observer;
    private ListContainer listContainer;
    private boolean isEnable = true;

    /**
     * ListAdapter的构造
     *
     * @param listBeans
     * @param slice
     * @param mlayoutType
     * @param observer
     */
    public ListAdapter(List<ListBean> listBeans, AbilitySlice slice, int mlayoutType, MyObserver observer) {
        this.listBeans = listBeans;
        this.slice = slice;
        this.mLayoutType = mlayoutType;
        this.observer = observer;
        vectorElement = new ShapeElement(slice, ResourceTable.Graphic_border);
    }

    public void setListContainer(ListContainer listContainer) {
        this.listContainer = listContainer;
    }

    public void setEnable(boolean isEnable) {
        this.isEnable = isEnable;
    }

    /**
     * 可重用ViewHolder类
     *
     * @since 2021-04-19
     */
    public class ListHolder {
        Image listImg;
        Text listText;
        BaseDependentLayout baseDirectionalLayout;

        ListHolder(Component component) {
            listImg = (Image) component.findComponentById(ResourceTable.Id_list_item_img);
            listText = (Text) component.findComponentById(ResourceTable.Id_list_item_text);
            baseDirectionalLayout = (BaseDependentLayout) component
                    .findComponentById(ResourceTable.Id_list_item_baseDirectional);
        }
    }

    @Override
    public int getCount() {
        return listBeans == null ? 0 : listBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return listBeans.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        component = null;
        ListHolder holder;
        ListBean listBean = listBeans.get(i);
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_list_item_layout,null,false);
            holder = new ListHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder = (ListHolder) cpt.getTag();
        }
        holder.listText.setText(listBean.getValue());
        holder.listImg.setPixelMap(listBean.getImgId());
        holder.baseDirectionalLayout.setEnable(isEnable);
        if (mLayoutType == FinalStaticBean.RANDIUBORDER) {
            holder.baseDirectionalLayout.setMarginsLeftAndRight(FinalStaticBean.NUM_50,FinalStaticBean.NUM_50);
            holder.baseDirectionalLayout.setMarginsTopAndBottom(FinalStaticBean.NUM_20,FinalStaticBean.NUM_20);
            holder.baseDirectionalLayout.setBackground(vectorElement);
            holder.baseDirectionalLayout.setEndElement(vectorElement);
        } else {
            if (listContainer != null) {
                holder.baseDirectionalLayout.setListContainer(listContainer);
            }
            holder.baseDirectionalLayout.setSlide(true);
            holder.baseDirectionalLayout.setComponents(ResourceTable.Id_left_component,
                    ResourceTable.Id_right_component,
                    ResourceTable.Id_include_component);
            if (observer != null) {
                holder.baseDirectionalLayout.setObserver(observer);
            }
        }
        return cpt;
    }
}
